import api from '../../api'
import tipBox from "../../plugins";

const state = {
    menus:[],
    types:[],
    products:[],
    fcid:"",
}
const actions = {
    loadTypeMenus({ commit,dispatch }) {
        api.loadDataByPids({
            appPlat: "m",
            pids: "117330"
        }).then(({ success, data }) => {
            if (!success) {
                return Promise.reject("请求失败");
            }
            commit("setmenus", data[117330].list);
            return data[117330].list[0];
        }).then((item)=>{
            dispatch("loadTypeList",{
                pid:item.maitKey
            });
            commit("setfcid",item.miniWallkey);
            dispatch("loadTypeProducts",{
                fcid:item.miniWallkey,
                sort:"pop"
            })
        }).catch((error) => {
            // console.log(error);
            tipBox.showMsg("数据加载失败","error",2000);
        })
    },
    loadTypeList({ commit },params) {
        api.loadDataByPid({
            appPlat:"m",
            pid:params.pid
        }).then(({ success, data:{ list } }) => {
            if (!success) {
                return Promise.reject("请求失败");
            }
            // this.homeData.likes = docs;
            commit("settypes", list)
        }).catch(error => {
            // console.log("请求失败", error);
            showMsg("数据加载失败","error",2000);
        });
    },
    loadTypeProducts({ state,commit,rootState  },params) {
        api.loadProductsByPid({
            fcid: params.fcid ,
            page: 1,
            sort: params.sort,
            cKey:"h5-cube",

        }).then(({ success, result:{ sortFilter,wall : { docs }} }) => {
            if (!success) {
                return Promise.reject("请求失败");
            }
            // this.homeData.likes = docs;
            commit("setproducts", docs);
            if(rootState.sortList.length==0){
                commit("setsortList", sortFilter,{
                    root: true
                });                
            }
        }).catch(error => {
            // console.log("请求失败", error);
            tipBox.showMsg("数据加载失败","error",2000);
        });
    }
}
const mutations = {
    setmenus(state,nv){
        state.menus = nv;
    },
    settypes(state,nv){
        state.types = nv;
    },
    setproducts(state,nv){
        state.products = nv;
    },
    setfcid(state,nv){
        state.fcid = nv;
    }
}

export default {
    namespaced: true, // 开启命名空间
    state,
    actions,
    mutations
}